﻿layui.use(['form', 'table', 'dtree', 'http', 'tag', 'popup', 'drawer', 'common'], function () {
    var form = layui.form;
    var table = layui.table;
    var $ = layui.$;
    var popup = layui.popup;
    var http = layui.http;
    var dtree = layui.dtree;
    var drawer = layui.drawer;
    var tag = layui.tag;
    var common = layui.common;
    var selectClsId = [];
    var deleteClsId = [];
    var selectBookShelfId = 0;
    var selectLibraryId = 0;
    var drawerDom;

    table.render({
        elem: '#dataTable',
        url: '/Bookshelf/GetBookshelfList',
        where: { LibraryId: $("#LibraryId").val() },
        height: 'full',
        page: true,
        skin: 'line',
        toolbar: '#toolbar',
        defaultToolbar: [{
            title: '刷新',
            layEvent: 'refresh',
            icon: 'layui-icon-refresh',
        }, 'filter', 'print', 'exports'],
        cols: [[
            { field: 'code', title: '书架号' },
            { field: 'libraryName', title: '图书室' },
            {
                title: '分类', templet: function (row) {
                    return '';
                }
            },
            { title: '操作', toolbar: '#rowbar' },
        ]],
        done: function (res, curr, count) {
            if (res.code === 1) {
                popup.toastError(res.msg);
            }
        }
    });

    // 表格上方的按钮
    table.on('toolbar(dataTable)', function (obj) {
        if (obj.event === 'add') {
            add();
        } else if (obj.event === 'refresh') {
            refresh();
        }
    });
    // 行 操作
    table.on('tool(dataTable)', function (obj) {
        if (obj.event === 'edit') {
            add(obj.data);
        }
        else if (obj.event === 'remove') {
            layer.confirm(`确定删除${obj.data.code}?`, { icon: 3, title: '提示' }, function (index) {
                var loading = layer.load(2);
                http.ajax({
                    url: '/Bookshelf/Delete',
                    data: { Id: obj.data.id },
                    success: function (res) {
                        layer.close(loading);
                        if (res.code === 0) {
                            popup.toastSuccess(res.msg);
                            layer.close(index);
                            refresh();
                        } else {
                            popup.toastError(res.msg);
                        }
                    },
                    error: function () {
                        layer.close(loading);
                        popup.toastError("请求失败，请稍后重试");
                    }
                });

            });
        }
        else if (obj.event === 'clsSet') {
            $("#selectCls").empty();
            selectClsId = [];
            deleteClsId = [];
            selectBookShelfId = obj.data.id;
            selectLibraryId = obj.data.libraryId;
            drawerDom = drawer.open({
                direction: "right",
                dom: ".layer-close-mask",
                distance: "60%",
                maskClose: false,
                callback: function () {
                    drawerDom.close();
                }
            });
        }
    });
    $("#btnClose").click(function () {
        drawerDom.close();
    })
    /**
     * 新增或编辑
     * @param {any} row
     */
    function add(row) {
        var title = '新增书架';
        if (row) {
            title = '编辑书架';
        }
        layer.open({
            type: 2,
            title: title,
            shade: 0.1,
            btn: ['确定', '取消'],
            area: ['500px', '400px'],
            content: "/Bookshelf/Add?id=" + (row ? row.id : 0),
            success: function (layero, index) {
                var body = layer.getChildFrame('body', index);
                body.find('#code').val(row.code)
                body.find('#id').val(row.id)
            },
            yes: function (index, layero) {
                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                iframeWin.save();
            }
        });
    }
    $("#search").click(function () {
        refresh();
    });
    /**刷新 */
    function refresh() {
        var data = { LibraryId: $("#LibraryId").val(), shelfName: $("#shelfName").val() };

        table.reload('dataTable', {
            where: data,
            page: {
                curr: 1
            }
        });
    }

    dtree.render({
        elem: "#tree",
        initLevel: "2",
        type: "all",
        line: true,
        ficon: ["1", "-1"],
        icon: ["0", "2"],
        method: 'get',
        dataStyle: 'layuiStyle',
        response: { message: "msg", statusCode: 0 },
        url: '/Classification/GetTree'
    });
    dtree.on("node('tree')", function (obj) {
        if (obj.param.nodeId !== '0' && obj.param.leaf) {
            if (selectClsId.indexOf(obj.param.nodeId) === -1) {
                selectClsId.push(obj.param.nodeId);
                deleteClsId.indexOf(obj.param.nodeId) > -1 && common.removeByIndex(deleteClsId, deleteClsId.indexOf(obj.param.nodeId))
                var html = '<span lay-id="' + obj.param.nodeId + '"  class="tag-item tag-item-normal">' + obj.param.context + '</span>';
                $("#selectCls").append(html);
                tag.render("selectCls");
            }
           
        }
    });
    tag.on('delete(selectCls)', function (data) {
        var temp = selectClsId[data.index];
        deleteClsId.indexOf(temp)===-1 &&  deleteClsId.push(temp);
        common.removeByIndex(selectClsId, data.index);
        console.log(deleteClsId)
        console.log(selectClsId)
    });

    $("#submit").click(function () {
        var loading = layer.load(2);
        http.ajax({
            url: '/Bookshelf/SetClass',
            data: { id: selectBookShelfId, libraryId: selectLibraryId, selectClsId: selectClsId, deleteClsId: deleteClsId },
            success: function (res) {
                layer.close(loading);
                popup.toastResult(res);
                if (res.code === 0) {
                    drawerDom.close();
                }
            }

        });

    });
});